package cn.yougou.controller;

import cn.yougou.common.AnalysisFileUtil;
import cn.yougou.common.DtoUtil;
import cn.yougou.common.EnumUtil;
import cn.yougou.dto.Dto;
import cn.yougou.dto.Query;
import cn.yougou.pojo.YgCoupons;
import cn.yougou.service.shopcart.YgCouponsService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author: DELL
 * @date: 2020/12/20 10:58
 * @description:
 */
@RestController
@RequestMapping("/coupons")
public class YgCouponsController {
    @Reference
    private YgCouponsService ygCouponsService;
    @Autowired
    private EnumUtil enumUtil;
    @Autowired
    private AnalysisFileUtil annotationsUtil;

    @GetMapping("/selCouponsTemp")
    public Dto selCouponsTemp(@RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
                              @RequestParam(value = "limit",required = false,defaultValue = "10") Integer limit,
                              String getRules,
                              String couponTitile,
                              Integer state
                              ){
    /**
     * 条件查询需要：标题 优惠卷类型 优惠卷状态
     * 需要查出的数据有：优惠卷id、优惠卷名称、介绍、优惠类型、优惠限制、减慢金额、每人限领、
     * 商品使用范围、优惠卷类型、优惠卷数量、状态、使用期限、最终过期时间
     * 需要特殊处理的数据有：无
     * */

        Query query= new Query(((page-1)*limit),limit,couponTitile,getRules,state);
        //获取总数
        query.setInfoCount(ygCouponsService.selCount(query)) ;
        //利用总数查询
        if(query.getInfoCount()>0){
            //查出数据 在service层就封装好数据
           List ygCouponsList= ygCouponsService.selCouponsTemp(query);
           return DtoUtil.returnDataListSuccess(ygCouponsList,query);
        }else{
            return DtoUtil.returnSuccess("4001");
        }

    }
    /**
     * 通过优惠卷id修改优惠卷
     * 如果没有则新增优惠卷
     * */
    @PostMapping("UpdataCoupen")
    public Dto UpdataCoupen(Integer id,//优惠卷id
             String coupontitile,//优惠卷名称
             String couponrulestitle,//介绍
             String couponrules,//优惠类型
             String couponrulesjudge,//优惠限制
             Double couponmoney,//优惠金额
             Integer repetitionget,//每人限领
             String scope,//使用范围
             String getrules,//优惠卷类型
             Integer couponscount,//优惠卷数量
             Integer couponsTime,//过期时间
             String stopdate,//最终过期时间
             Integer state,//状态
                            @RequestParam(value = "LinkcouponCategoryList[]", required = false)   Integer []  LinkcouponCategoryList,//分类列表
                            @RequestParam(value = "LinkgoodsList[]", required = false) Integer [] LinkgoodsList,
                            @RequestParam(value = "forbidScope[]", required = false) Integer [] forbidScope ){//商品列表


       // System.out.println(new YgCoupons(id,coupontitile,enumUtil.returnGetRulesEnumByValue(getrules).toString(),couponrulesjudge,annotationsUtil.returnStringByList(LinkcouponCategoryList),annotationsUtil.returnStringByList(LinkgoodsList),enumUtil.returnCouponRulesEnumByValue(couponrules).toString(),couponrulestitle,couponrulesjudge,couponmoney,repetitionget,null,null,stopdate ,couponscount,state,couponsTime,annotationsUtil.returnStringByList(forbidScope)).toString());
           try {
          int result=  ygCouponsService.insertAndUpdata(new YgCoupons(id,coupontitile,enumUtil.returnGetRulesEnumByValue(getrules).toString(),couponrulesjudge,annotationsUtil.returnStringByList(LinkcouponCategoryList),annotationsUtil.returnStringByList(LinkgoodsList),enumUtil.returnCouponRulesEnumByValue(couponrules).toString(),couponrulestitle,couponrulesjudge,couponmoney,repetitionget,null,null,stopdate ,couponscount,state,couponsTime,annotationsUtil.returnStringByList(forbidScope)));
          if(result >0){
              return DtoUtil.returnSuccess("200");
          }else{
              return DtoUtil.returnSuccess("4001");
          }
      }catch (Exception e) {
               e.printStackTrace();
          return DtoUtil.returnSuccess("4001");
      }


    }
    /**
     * 获得商品列表和商品类型列表
     * */
    @GetMapping("getScopeList")
    public Dto returnCommodityAndCategoryList(){
       Map map= ygCouponsService.returnCommodityAndCategoryList();
        return DtoUtil.returnDataSuccess(map);
    }
    @PostMapping("delete")
    public Dto delete(@RequestBody YgCoupons ygCoupons){
        boolean result = ygCouponsService.deleteById(ygCoupons.getId());
        if(result){
            return DtoUtil.returnSuccess("200");
        }else{
            return DtoUtil.returnSuccess("4001");
        }

    }
}
